<?php
class JoinAction extends CAction{
	
	public function run(){
		$model = new User;
		if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		if(isset($_POST['User']))
		{
			$model->attributes=$_POST['User'];
			$password = $model->password;
			if($model->save() && $this->_autoLogin($model->username,$password))
				$this->getController()->redirect(Yii::app()->createUrl('sns/init'));
		}
		$this->getController()->render('register',array('model'=>$model));
	}
	
	private function _autoLogin($username,$password){
		$identity = new UserIdentity($username,$password);
		$identity->authenticate();
		if($identity->errorCode===UserIdentity::ERROR_NONE){
			Yii::app()->user->login($identity);
			return true;
		}
		return false;
	}
}
?>